﻿using AutoMapper; //AutoMapper
using PcProject.Application.Service.AutoMapper;
using PcProject.Domain.IRepository;
using PcProject.Domain.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PcProject.Infrastructure.Repository;

namespace PcProject.Application.Service.Products
{
    /// <summary>
    /// 商品模块
    /// </summary>
    public class ProductService:IProductService
    {
        /// <summary>
        /// 构造函数依赖注入
        /// </summary>
        private IProductRepository _productRepository;
        private IMapper _mapper;
        /// <summary>
        /// 函数依赖注入
        /// </summary>
        /// <param name="productRepository"></param>
        /// <param name="mapper"></param>
        public ProductService(IProductRepository productRepository,IMapper mapper)
        {
            _productRepository = productRepository;
            _mapper = mapper;
        }


        /// <summary>
        /// 显示+分页+查询
        /// </summary>
        /// <param name="pname">查询参数</param>
        /// <param name="pageIndex">页数</param>
        /// <param name="pageSize">条数</param>
        /// <param name="totalCount">总条数</param>
        /// <returns></returns>
        public List<Product> GetProductsAsync(string pname, int pageIndex, int pageSize, out int totalCount)
        {
            return _productRepository.GetProductsAsync(pname, pageIndex, pageSize, out totalCount);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="obj">表单参数</param>
        /// <returns></returns>
        public async Task<int> InsertAsync(ProductDto obj)
        {
            Product a = _mapper.Map<Product>(obj);
            return await _productRepository.InserAsync(a);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<int> DeleAsync(int id)
        {
            return await _productRepository.DeleAsync(id);
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<Product> FillAsync(int id)
        {
            return await _productRepository.FillAsync(id);
        }

        /// <summary>
        /// 商品详情
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public ProductDto FillsAsync(int id)
        {
            ProductDto product = _mapper.Map<ProductDto>(_productRepository.FillsAsync(id));
            return product;
        }

        /// <summary>
        /// 加入浏览记录
        /// </summary>
        /// <param name="obj">参数</param>
        /// <returns></returns>
        public int AddBrowse(Browse obj)
        {
            return _productRepository.AddBrowse(obj);
        }

        /// <summary>
        /// 根据ID查找商品信息
        /// </summary>
        /// <param name="b">参数</param>
        /// <returns></returns>
        public List<Product> Products(string b)
        {
            return _productRepository.Products(b);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="obj">表单参数</param>
        /// <returns></returns>
        public async Task<int> UptAsync(ProductDto obj)
        {
            Product a = _mapper.Map<Product>(obj);
            return await _productRepository.UptAsync(a);
        }

        /// <summary>
        /// 砍价修改
        /// </summary>
        /// <param name="obj">参数</param>
        /// <returns></returns>
        public int Upt(Product obj)
        {
            return _productRepository.Upt(obj);
        }

        /// <summary>
        /// 加入回收站
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<int> RecycAsync(int id)
        {
            Product product = await _productRepository.GetByIdAsyncs(id);
            if (product == null)
            {
                throw new InvalidOperationException();
            }
            product.AddRecy();
            return await _productRepository.RecycAsync(id);
        }

        /// <summary>
        /// 回收站显示分页
        /// </summary>
        /// <param name="pageIndex">页数</param>
        /// <param name="pageSize">条数</param>
        /// <param name="totalCount">总条数</param>
        /// <returns></returns>
        public List<Product> GetRecycAsync(int pageIndex, int pageSize, out int totalCount)
        {
            return _productRepository.GetRecycAsync(pageIndex, pageSize, out totalCount);
        }

        /// <summary>
        /// 还原回收站
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<int> ResetAsync(int id)
        {
            Product product = await _productRepository.GetByIdAsyncs(id);
            if (product == null)
            {
                throw new InvalidOperationException();
            }
            product.OutRecy();
            return await _productRepository.ResetAsync(id);
        }

    }
}
